package com.sunday.electric.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.electric.R;
import com.sunday.electric.base.BaseActivity;
import com.sunday.electric.entity.Member;
import com.sunday.electric.http.ApiClient;
import com.sunday.electric.utils.SharePerferenceUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/1/9.
 */
public class LoginActivity extends BaseActivity {
    @Bind(R.id.user_account)
    EditText userAccount;
    @Bind(R.id.user_password)
    EditText userPwd;
    @Bind(R.id.checkbox)
    CheckBox checkBox;
    private int userType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    userType = 1;
                }else{
                    userType = 2;
                }
            }
        });
    }

    @OnClick(R.id.login_btn)
    void loginButton(){
        String userName = userAccount.getText().toString();
        String password = userPwd.getText().toString();
        showLoadingDialog(0);
        Call<ResultDO<Member>> call = ApiClient.getIMAdapter().login(userName,password,userType);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call,Response<ResultDO<Member>> response) {
                dissMissDialog();
                ResultDO<Member> resultDO = response.body();
                if(resultDO.getCode()==0){
                    ToastUtils.showToast(mContext,"登录成功");
                    intent = new Intent(mContext,MainActivity.class);
                    startActivity(intent);
                    SharePerferenceUtils.getIns().putBoolean(Constants.IS_LOGIN,true);
                    SharePerferenceUtils.getIns().saveOAuth(resultDO.getResult());
                    finish();
                }else{
                    ToastUtils.showToast(mContext,"登录失败");
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext,"登录异常");
            }



        });
    }
}
